//
//  HJNewAddViewController.m
//  Sneakers
//
//  Created by 胡光健 on 2017/9/25.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJNewAddViewController.h"

@interface HJNewAddViewController ()<UITextFieldDelegate,HJCityPickerDelegate>
@property(nonatomic,strong)NSArray * placeHolderArr;
@property(nonatomic,strong)NSString * name;
@property(nonatomic,strong)NSString * mobile;
@property(nonatomic,strong)NSString * address;
@property(nonatomic,strong)NSString * detail_address;
@property(nonatomic,strong)NSNumber * is_default;

@property(nonatomic,assign)BOOL isSelecte;
@end

@implementation HJNewAddViewController

-(NSArray *)placeHolderArr {
    if (!_placeHolderArr) {
        _placeHolderArr = @[@"姓名",@"手机号码",@"省、市、区",@"地址（街道、小区、门牌号）"];
    }
    return _placeHolderArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configBottom];
    self.is_default = @1;
    self.isSelecte = NO;
}

-(void)configTableView {
    self.tableViewStyle = UITableViewStylePlain;
    [super configTableView];
    CGFloat H;
    if (mScreenHeight<812) {
        H = mScreenHeight -60 -64;
    }else {
        H = mScreenHeight-60-TabbarHeight-X_Bottom_Space;
    }
    self.tableView.frame = CGRectMake(0, 0, mScreenWidth, H);
    self.tableView.scrollEnabled= NO;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row==4) {
        [self configLabelWithSwitch:cell];
    }else if (indexPath.row==2){
        [self configTextFieldWithCell:cell text:@"" placeHolder:self.placeHolderArr[indexPath.row] tag:3 btnBool:NO];
    }else {
       [self configTextFieldWithCell:cell text:@"" placeHolder:self.placeHolderArr[indexPath.row] tag:1+indexPath.row btnBool:YES];
    }
    
    UIView * line = [[UIView alloc]initWithFrame:CGRectMake(15, 79, mScreenWidth-30, 1)];
    line.backgroundColor = HJColorRGB(0);
    line.alpha =0.05;
    [cell.contentView addSubview:line];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

-(void)configTextFieldWithCell:(UITableViewCell*)cell text:(NSString*)text placeHolder:(NSString *)placeHolder tag:(NSInteger)tag    btnBool:(BOOL)btnBool {
    UITextField * textField = [[UITextField alloc]init];
    textField.frame = CGRectMake(15, 25, mScreenWidth - 30, 30);
    textField.text = text;
    textField.placeholder = placeHolder;
    textField.delegate = self;
    textField.tag = tag;
    textField.textColor = HJColorRGB(0);
    textField.font = [UIFont fontWithName:@"PingFangSC-Regular" size:mScreenWidth/414 *15.0f];
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.autocorrectionType =UITextAutocorrectionTypeYes;
    textField.textAlignment = NSTextAlignmentLeft;
    textField.adjustsFontSizeToFitWidth = YES;
    textField.keyboardType =UIKeyboardTypeDefault;
    textField.returnKeyType =UIReturnKeyDone;
    
    [cell.contentView addSubview:textField];
    
    if (!btnBool) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(15, 25, textField.width, textField.height);
        [btn addTarget:self action:@selector(selecteCityPickerBtn:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:btn];
    }else {
        return;
    }
}
-(void)selecteCityPickerBtn:(UIButton *)btn {
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];

    self.needPopSelectView = YES;
    self.pickView.hidden = NO;
    [self.pickView.cityPicker reloadAllComponents];
}

-(void)PickerViewDidClickOKButton:(NSString *)PickerTitle {
   self.pickView.hidden = YES;
    UITextField *textField = [self.view viewWithTag:3];
    textField.text = PickerTitle;
    self.address = PickerTitle;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField {
    self.pickView.hidden = YES;
    if (textField.tag==3) {
        [textField resignFirstResponder];
        return;
    }

}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    self.pickView.hidden = YES;
    [textField resignFirstResponder];
    return YES;
}
-(void)configLabelWithSwitch:(UITableViewCell*)cell {
    UILabel * label = [self configWithLabel:@"设为默认收货地址" textColor:HJColorRGB(0) textAlignment:NSTextAlignmentLeft frame:CGRectMake(15,  25, 140, 30) font:[UIFont fontWithName:@"Regular" size:mScreenWidth/414 *17]];
    [cell.contentView addSubview:label];
    
    LQXSwitch * swi = [[LQXSwitch alloc]initWithFrame:CGRectMake(mScreenWidth-15-50, 25, 42, 28) onColor:HJImage(@"open") offColor:HJImage(@"swi") font:nil ballSize:30];
    [swi setOn:YES animated:YES];
    [swi addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    [cell.contentView addSubview:swi];
}

-(void)switchAction:(LQXSwitch *)swi {
    if (swi.on) {
        self.is_default = @1;
    }else {
        self.is_default = @0;
    }
}


-(void)configBottom {
    CGFloat H;
    if (mScreenHeight<812) {
        H = mScreenHeight -60 -64;
    }else {
        H = mScreenHeight-60-TabbarHeight-X_Bottom_Space;
    }
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, H, mScreenWidth, 60);
    [btn setTitle:@"保存" forState:UIControlStateNormal];
    [btn setTitleColor:HJColorRGB(0) forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    [btn setBackgroundColor:HJColorRGB(255)];
    btn.layer.shadowOpacity = 0.2;
    btn.layer.shadowRadius = 3;
    btn.layer.shadowOffset = CGSizeZero;
    btn.layer.shadowColor = [UIColor blackColor].CGColor;
    [btn addTarget:self action:@selector(saveBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    [self.view bringSubviewToFront:btn];
}
-(void)saveBtn:(UIButton*)btn {
    UITextField * name = [self.view viewWithTag:1];
    if (name.text.length ==0 ) {
        [self showHint:@"请输入姓名"];return;
    }
    UITextField * mobile = [self.view viewWithTag:2];
    if (![CommonTools isMobileNumber:mobile.text]) {
        [self showHint:@"请输入正确手机号"];return;
    }
    
    if (self.address.length ==0 ) {
        [self showHint:@"请选择省、市、区"];return;
    }
    UITextField * detail_address = [self.view viewWithTag:4];
    if (detail_address.text.length ==0 ) {
        [self showHint:@"请输入详细地址"];return;
    }
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"name"] = name.text;
    params[@"mobile"] = mobile.text;
    params[@"address"] = self.address;
    params[@"detail_address"] = detail_address.text;
    params[@"is_default"] = self.is_default;
    
    [TZHttpTool post:AddressAPI params:params success:^(NSDictionary *result) {
        [self showSuccessHUDWithStr:result[@"msg"]];
        if (self.loadBlock) {
            self.loadBlock();
        }
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.02 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
    } failure:^(NSString *msg) {
        [self showErrorHUDWithError:msg];
    }];
}
@end
